package vno.sci.window
{
	import com.hurlant.util.der.Integer;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	
	import org.aswing.JWindow;
	
	import vno.fux.res.pool.BitmapAttacher;
	import vno.sci.event.MonologEvent;
	import vno.sci.event.ToolbarEvent;

	public final class UILayer extends Sprite
	{
		private var holder:Object = {};// array of out-of-nowhere sprites
		private var data:Object = {};// array of MonologController
//		private var toolbar:Object = {};// array of toolbar buttons.
		
		public function UILayer ()
		{
			super();
			
			var ttt:Toolbar = new Toolbar(this);
			ttt.setSizeWH(200,40);
			var skin:Bitmap = new Bitmap(new BitmapData(100,100,false,0x13579b));
			var skin2:Bitmap = new Bitmap(new BitmapData(100,100,false,0x79acae));
			var skin3:Bitmap = new Bitmap(new BitmapData(100,100,false,0xace000));
			var prevBtn:MyButton = new MyButton("PrevBtn", null, "Prev",skin,skin2,skin3);
			
			var skin4:Bitmap = new Bitmap(new BitmapData(100,100,false,0xace000));
			var skin5:Bitmap = new Bitmap(new BitmapData(100,100,false,0x00ee00));
			var skin6:Bitmap = new Bitmap(new BitmapData(100,100,false,0xace0ee));
			var nextBtn:MyButton = new MyButton("NextBtn", null, "Next",skin4,skin5,skin6);
			ttt.pushButton(prevBtn);
			ttt.pushButton(nextBtn);
			ttt.show();
			ttt.addEventListener(ToolbarEvent.CLICK, onToolbarClick);
			
			
			
		}
		
		private function onCloseWindow(event:MonologEvent):void
		{
			// TODO Auto-generated method stub
			this.deleteWindow(event.id);
		}
		
		protected function onToolbarClick(event:ToolbarEvent):void
		{
		}
		/**
		 * Create a Window
		 * @param id
		 * id must be unique;
		 * @param params
		 * a can be associative array or an XML with the following properties.
		 * title
		 <p>text</p>
		 <p>htmlText</p>
		 <p>titleHeight</p>
		 <p>buttonHeight</p>
		 <p>windowWidth</p>
		 <p>windowHeight</p>
		 <p>textWidth</p>
		 <p>textHeight</p>
		 <p>x</p>
		 <p>y</p>
		 <p>visible</p>
		 * @param frameBackground
		 * frame background picture
		 * @param textBackground
		 * text background picture
		 * @see vno.fux.res.pool.BitmapAttacher
		 */
		public function newWindow(id:uint, params:Object, frameBackground:BitmapAttacher = null, textBackground:BitmapAttacher = null):void
		{
			if (holder[id])
				throw new Error("Window Id already exists");
			var m:Monolog = new Monolog(id, this, params, frameBackground, textBackground);
			holder[id] = this.getChildAt(this.numChildren-1);
			data[id] = m.c;
			//newly added.
			m.c.addEventListener(MonologEvent.CLOSING,onCloseWindow);
		}
		/**
		 * identical with newWindow();
		 * @see #newWindow()
		 */		
		public function createWindow(id:uint, params:Object, frameBackground:BitmapAttacher = null, textBackground:BitmapAttacher = null):void
		{
			newWindow(id,params, frameBackground, textBackground);
		}
		/**
		 * delete an existing Window by id;
		 * do nothing if id doesn't exist;
		 * @param id:uint
		 * 
		 */		
		public function deleteWindow(id:uint):void
		{
			if (holder[id])
			{
				this.removeChild(holder[id]);
				holder[id] = null;
				data[id] = null;
			}
		}
		/**
		 * identical with deleteWindow()
		 * @see #deleteWindow() 
		 */		
		public function removeWindow(id:uint):void
		{
			deleteWindow(id);
		}
		public function changeWindow(id:uint, params:Object):Boolean
		{
			if (holder[id])
			{
				data[id].changeModel(params);
				return true;
			}
			return false;
		}
		/**
		 * this class dispatchs MonologEvent
		 * @see vno.sci.event.MonologEvent 
		 */		
		override public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			for each (var item in data)
			{
				item.addEventListener(type, listener, useCapture, priority, useWeakReference);
			}
		}
		public function alignCenter(id,stageWidth,stageHeight):void
		{
			var w:Number = stage.width;
			var h:Number = stage.height;
			var m = data[id]
			var mw = m.stageWidth;
			var mh = m.stageHeight;
			var a:Object = {};
			a["x"] = (w-mw)/2;
			a["y"] = (h-mh)/2;
			m.c.changeModel(a);
		}
	}
}